package com.zyht.website.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wx.entity.SysIpWhite;
import com.wx.entity.SysIpWhiteExample;
import com.wx.entity.SysIpWhiteExample.Criteria;
import com.wx.flag.IPWhiteEnum;
import com.wx.service.SysIPWhiteService;
import com.wx.util.JsonUtil;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
/**
 * 登录IP白名单
 * @author InkYi
 *
 */
@Controller
@RequestMapping("admin/ipwhite")
public class SysIpWhiteController {
	
	//private Logger logger = LoggerFactory.getLogger(getClass());
	
	private final String VIEW_PREFIX = "admin/ipwhite/";

	@Resource
	private SysIPWhiteService sysIPWhiteService;
	
	
	@RequestMapping("index")
	@RequiresPermissions(value={"admin:ipwhite:index"})
	public String index(String description,Integer currentPage, Model model){
		
		SysIpWhiteExample example = new SysIpWhiteExample();
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(description)){
			criteria.andDescriptionLike("%"+description+"%");
		}
		
		String pageSizeStr = PropertiesUtils.getConfig("paganation_page_size");
		int pageSize = Integer.parseInt(pageSizeStr);
		if(currentPage==null || currentPage < 1){
			currentPage = 1;
		}
		
		PageHelper.startPage(currentPage, pageSize);
		Page<SysIpWhite> ips = (Page<SysIpWhite>) sysIPWhiteService.selectByExample(example);
		
		com.zyht.common.util.Page<SysIpWhite> page = new com.zyht.common.util.Page<>();
		page.setPernum(pageSize);
		page.setCurrentPage(currentPage);
		page.setRecordList(ips.getResult());
		page.setTotal((int)ips.getTotal());
		
		model.addAttribute("page", page);
		model.addAttribute("description", description);
		
		return makeViewName("index");
	}
	
	
	@RequestMapping("edit")
	@RequiresPermissions(value={"admin:ipwhite:edit"})
	public String edit(Integer id, Model model){
		SysIpWhite bean = null;
		if(id!=null){
			bean = sysIPWhiteService.selectByPrimaryKey(id);
		}
		model.addAttribute("bean", bean);
		return makeViewName("add");
	}
	
	@RequestMapping("persis")
	@RequiresPermissions(value={"admin:ipwhite:persis"})
	public String persis(SysIpWhite bean, Model model){
		if(StringUtils.isEmpty(bean.getBeginIp())){
			//起始IP不能为空
			model.addAttribute("msg", "起始IP不能为空");
			model.addAttribute("redirect", "admin/ipwhite/index");
			return "message";
		}
		if(StringUtils.isEmpty(bean.getEndIp())){
			//结束IP不能为空
			model.addAttribute("msg", "结束IP不能为空");
			model.addAttribute("redirect", "admin/ipwhite/index");
			return "message";
		}
		if(bean.getId()==null){
			sysIPWhiteService.insert(bean);
		}else{
			sysIPWhiteService.updateByPrimaryKey(bean);
		}
		
		model.addAttribute("msg", "操作成功");
		model.addAttribute("redirect", "admin/ipwhite/index");
		return "message";
	}
	
	@RequestMapping("del")
	@RequiresPermissions(value={"admin:ipwhite:del"})
	public String del(Integer id, Model model){
		if(id!=null){
			sysIPWhiteService.deleteByPrimaryKey(id);
		}
		model.addAttribute("msg", "删除成功");
		model.addAttribute("redirect", "admin/ipwhite/index");
		return "message";
	}
	
	@RequestMapping("setValid")
	@RequiresPermissions(value={"admin:ipwhite:setValid"})
	public @ResponseBody String ajaxSetValid(Integer id,String isValid) {
		if(id==null || isValid==null){
			return jsonResult("数据错误", false);
		}
		SysIpWhite bean = sysIPWhiteService.selectByPrimaryKey(id);
		if (bean == null) {
			return jsonResult("数据不存在", false);
		}
		if(isValid.equals("0")){
			bean.setIsValid(IPWhiteEnum.IS_VALID_NO.getValue());
		}else{
			bean.setIsValid(IPWhiteEnum.IS_VALID_YSE.getValue());
		}
		sysIPWhiteService.updateByPrimaryKey(bean);
		return jsonResult("成功", true);
	}
	
	
	private String makeViewName(String page){
		return VIEW_PREFIX.concat(page);
	}
	
	private String jsonResult(String msg, Boolean success) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("msg", msg);
		result.put("success", success);
		return JsonUtil.Object2Json(result);
	}
	
	
	
}
